package com.century.sjt.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.bumptech.glide.Glide;
import com.century.sjt.R;
import com.century.sjt.util.Constant;
import com.century.sjt.util.SrcUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by pig33 on 15-06-19.
 */
public class NewsListViewAdapter extends BaseAdapter {

    private List<Map<String, Object>> dataList;
    private LayoutInflater mInflater;
    private RequestQueue mQueue;
    private Context mContext;

    public NewsListViewAdapter(Context context, RequestQueue mQueue, List<Map<String, Object>> dataList){
        mInflater= LayoutInflater.from(context);
        this.mQueue=mQueue;
        this.dataList=dataList;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        Map<String,Object> item=null;
        if(null!=dataList){
            item=dataList.get(position);
        }
        return item;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.home_item_tab3,null);
            holder=new ViewHolder();
            holder.newsTitle= (TextView) convertView.findViewById(R.id.tv_home_tab_text);
            holder.newsImg= (ImageView) convertView.findViewById(R.id.iv_home_tab1_img);
            holder.newsDate=(TextView)convertView.findViewById(R.id.tv_shouye_tab1_date);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }

        holder.newsTitle.setText(SrcUtil.tOHTML(dataList.get(position).get("text").toString()));
        holder.newsDate.setText((String)dataList.get(position).get("createDate"));
        String imgId= (String) dataList.get(position).get("image");
//        Log.e("新闻列表图片", imgId);
//        String imgUrl= Constant.ImageHost_Big + imgId;
        String imgUrl= Constant.BaseWebHost + imgId;
        Glide.with(mContext).load(imgUrl).into(holder.newsImg);
        return convertView;
    }

    private static class ViewHolder {
        private ImageView newsImg;
        private TextView newsTitle;
        private TextView newsDate;
    }
}
